package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 25169
 */
@RestController
@RequestMapping("/eduservice/indexfront")
public class IndexFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduTeacherService teacherService;

    /**
     * 查询前八条热门课程,前四名师
     * @return
     */
    @Cacheable(key = "'selectTeacherAndCourseList'",value = "teacherandcourse")
    @GetMapping("index")
    public R index(){
        LambdaQueryWrapper<EduCourse> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(EduCourse::getId).last("limit 8");
        List<EduCourse> listCourse = courseService.list(queryWrapper);

        LambdaQueryWrapper<EduTeacher> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.orderByDesc(EduTeacher::getId).last("limit 4");
        List<EduTeacher> listTeacher = teacherService.list(queryWrapper1);
        return R.ok().data("listCourse",listCourse).data("listTeacher",listTeacher);
    }
}
